package com.njtoyo.taxi.admin.rest.controller.business.operation_rule;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.rest.presenter.business.operation_rule.RideCancelRulePresenter;
import com.njtoyo.taxi.admin.service.business.operation_rule.RideCancelRuleService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 取消规则
 */
@RestController
@RequestMapping("business/operation-rule/ride-cancel")
public class RideCancelRuleController {

    @Autowired
    private RideCancelRuleService rideCancelRuleService;


    @RequestMapping("/page")
    public RestResult<IPage<RideCancelRulePresenter>> page(@ModelAttribute("admin") AdminUser adminUser,
                                                           @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                           @RequestParam(value = "size", required = false, defaultValue = "15") Integer size,
                                                           @RequestParam(value = "driverRegionId", required = false) Long driverRegionId) {
        return rideCancelRuleService.page(adminUser, current, size, driverRegionId);
    }


    @PostMapping("/add")
    public RestResult add(@RequestBody RideCancelRulePresenter rideCancelRulePresenter) {
        System.out.println(JSON.toJSONString(rideCancelRulePresenter));
        RestResult restResult = verityForm(rideCancelRulePresenter);
        if (ObjectUtil.isNotNull(restResult)) {
            return restResult;
        }
        return rideCancelRuleService.add(rideCancelRulePresenter);
    }

    @PostMapping("/update")
    public RestResult update(@RequestBody RideCancelRulePresenter rideCancelRulePresenter) {
        RestResult restResult = verityForm(rideCancelRulePresenter);
        if (ObjectUtil.isNotNull(restResult)) {
            return restResult;
        }
        return rideCancelRuleService.update(rideCancelRulePresenter);
    }

    private RestResult verityForm(RideCancelRulePresenter rideCancelRulePresenter) {
        if (ObjectUtil.isNull(rideCancelRulePresenter.getDriverRegionId())) {
            return RestResult.failed("请选择司机运营区");
        }
        if (ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getCancelMinute())) {
            return RestResult.failed("请填写[实时单]叫单后几分钟内免费取消");
        }
        if (ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getCancelDailyTime())) {
            return RestResult.failed("请填写[实时单]每天无责取消次数");
        }
        if (ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getCancelFreeStart()) || ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getCancelFreeEnd())) {
            return RestResult.failed("请填写[实时单]每天可无责取消的时间");
        }
        if (ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getLadderOneMin()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getLadderOneAmount()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getLadderTwoMin()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getLadderTwoAmount()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getLadderThreeMin()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getLadderThreeAmount()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getRealtime().getLadderFourAmount())) {
            return RestResult.failed("请完善[实时单]递增规则");
        }
        if (ObjectUtil.isNull(rideCancelRulePresenter.getAppointment().getDriverUnreached().getCancelTime())) {
            return RestResult.failed("请填写[预约单]司机达到前多少分钟内取消收费");
        } else if (ObjectUtil.isNull(rideCancelRulePresenter.getAppointment().getDriverUnreached().getAmount())) {
            return RestResult.failed("请填写[预约单]司机达到前多少分钟内取消收费金额");
        } else if (ObjectUtil.isNull(rideCancelRulePresenter.getAppointment().getDriverReached().getLadderOneMin()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getAppointment().getDriverReached().getLadderOneAmount()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getAppointment().getDriverReached().getLadderTwoMin()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getAppointment().getDriverReached().getLadderTwoAmount()) ||
                ObjectUtil.isNull(rideCancelRulePresenter.getAppointment().getDriverReached().getLadderTwoAmount()) ) {
            return RestResult.failed("请完善[预约单]司机达到后取消收费规则");
        }
        return null;

    }


}
